export default {
  /**
   * 格式化时间
   * @param js时间对象
   * @return string '2017-06-08 15:24'
   * */
  formatDate (ts) {
    const jsDate = new Date(ts)
    const year = jsDate.getFullYear()
    const month = this.one2two(jsDate.getMonth() + 1) // 0-11
    const date = this.one2two(jsDate.getDate()) // 1-31
    const hour = this.one2two(jsDate.getHours()) // 0-23
    const minute = this.one2two(jsDate.getMinutes()) // 0-59
    const second = jsDate.getSeconds() // 0-59
    return `${year}-${month}-${date} ${hour}:${minute}`
  },
  /**
   * 将year,month,date,hour,minute如果一位在前方补0
   * */
  one2two (item) {
    return item.toString().length === 1 ? ('0' + item) : item
  },
  getYear (jsDate) {
    return this.one2two(jsDate.getFullYear())
  },
  getMonth (jsDate) {
    return this.one2two(jsDate.getMonth() + 1)
  },
  getDate (jsDate) {
    return this.one2two(jsDate.getDate())
  },
  getHours (jsDate) {
    return this.one2two(jsDate.getHours())
  },
  getMinutes (jsDate) {
    return this.one2two(jsDate.getMinutes())
  },
  getSeconds (jsDate) {
    return this.one2two(jsDate.getSeconds())
  },
  /**
   * 获取当前时间的之前或者之后几天的时间对象
   * @param Date 当天日期对象
   * @param Number 天数
   * @return 目标当天的日期对象
   * */
  getBeforeAfter (dateObj, days) {
    const tempDate = new Date()
    const targetdayMlliseconds = dateObj.getTime() + 1000 * 60 * 60 * 24 * days
    tempDate.setTime(targetdayMlliseconds)
    return tempDate
  },
  /**
   * 根据时间字符串获取时间对象
   * @param String 2018-12-01
   * @return Date
   * */
  str2Date (str) {
    const dayArray = str.split('-')
    const year = dayArray[0]
    const month = dayArray[1] - 1
    const date = dayArray[2]

    // 根据后台返回的当天日期，构造当天的日期对象
    const tempDate = new Date()
    tempDate.setFullYear(year)
    tempDate.setMonth(month)
    tempDate.setDate(date)

    return tempDate
  },
  /**
   * 获取当前时间对象
   * @param String 2018-12-01
   * @return Date
   * */
  getNowDate () { 
    var date = new Date(); 
    var f1 = "-"; 
    var year = date.getFullYear();
    var month = date.getMonth() + 1;
    var date = date.getDate();
    if( month >= 1 && month <= 9 ) { 
      month = "0" + month;
    }   
    if( date >= 0 && date <= 9 ) { 
      date = "0" + date;
    }   
    var currentdate = year + f1 + month + f1 + date;   
    return currentdate
  }


}
